﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Preload : MonoBehaviour {

	AsyncOperation progress = null;
	Image progressBar;
	float progressFill = 0;
	public static float currentProgress = 0;
	public static bool loadingFinished = false;

	void Start () 
	{
		progressBar = GameObject.Find("ProgressBar").GetComponent<Image>();
		//Invoke("LoadLevel",2f);
		StartCoroutine("LoadScene");
	}

	IEnumerator LoadScene()
	{

		yield return new WaitForSeconds(0.5f);

		float timeLoading = 0;

		while(currentProgress < 1)
		{
			progressBar.fillAmount = currentProgress;
			timeLoading += Time.deltaTime;
			yield return timeLoading;
		}

		//yield return progress;
//		if(timeLoading < 1 && loadingFinished)
//		{
//			progressBar.fillAmount = 0;
//			while(progressBar.fillAmount < 1)
//			{
//				progressBar.fillAmount += 0.02f;
//				yield return Time.deltaTime;
//			}
//		}
		progressBar.fillAmount = 1;
		while(!loadingFinished)
			yield return null;
		yield return new WaitForSeconds(0.2f);
		progress = Application.LoadLevelAsync("MainMenu");
	}
}
